<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$mode = $_GET["mode"];
if($mode == "update"){
	$getListPet = $managerIUDS->queryData("*", "pet", " WHERE pet_id = ".$_GET["pet_id"], "1", "=");
	$listOwner = $managerIUDS->queryData("fname,lname", "owner", " WHERE owner_id = ".$getListPet["owner_id"], "1", "");
	$listSpecies = $managerIUDS->queryData("species_id,species_name","species","","2","");
	$listBreed = $managerIUDS->queryData("breed_id,breed_name","breed"," WHERE species_id = ".$getListPet["species_id"], "2", "");
	//breed
}else{
	$listOwner = $managerIUDS->queryData("owner_id,fname,lname","owner","","2","");
	$listSpecies = $managerIUDS->queryData("species_id,species_name","species","","2","");
	$listBreed = $managerIUDS->queryData("breed_id,breed_name","breed","","2","");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Pet Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">

			/*$(function() {
				$( "#species_id" ).combobox(

				);
				$( "#breed_id" ).combobox();
				$( "#owner_id" ).combobox();
			});*/
			function removeOptionLast(){
			  	var selectBox =  $('#breed_id').get(0);
  				 for(var i=selectBox.options.length-1;i>=1;i--){
  						selectBox.remove(i);
				 } 

			}
			$(document).ready(function(){ 
				$("#save_data").click(function(){
					var result = true;
					if($("#pet_name").val()=="" || $("#pet_name").val()==null){
						alert("กรุณากรอกชื่อสัตว์เลี้ยง");
						$("#pet_name").focus(); 
						result = false
					}
					else if($("#pet_age").val()=="" || $("#pet_age").val()==null){
						alert("กรุณากรอกอายุสัตว์เลี้ยง");
						$("#pet_age").focus(); 
						result = false
					}
					else if($("#species_id").val()=="" || $("#species_id").val()==null){
						alert("กรุณาเลือกประเภทของสัตว์เลี้ยง");
						$("#pet_age").focus(); 
						result = false
					}
					else if($("#breed_id").val()=="" || $("#breed_id").val()==null){
						alert("กรุณาเลือกสายพันธุ์ของสัตว์เลี้ยง");
						$("#pet_age").focus(); 
						result = false
					}else if($("#owner_id").val()=="" || $("#owner_id").val()==null){
						alert("กรุณาเลือกเจ้าของสัตว์เลี้ยง");
						$("#owner_id").focus(); 
						result = false
					}
					//owner_id
					
					if(result){
						$("#frm1").submit();
					}
				});
				$("#species_id").change(function(){
					 removeOptionLast();
					$.ajax({
						  url: "../actions/breed_action.php",
						  type: "GET",
						  data: {species_id : $("#species_id").val(),mode : "listBreedBySpeciesId"},
						  contentType: "application/json; charset=utf-8",
						  dataType: "json",
						  async : false,
						  cache : false,
						  success: function(data){
							//var str="";
							//alert(data);
							if(data.breedData.length>0){
								var selectBreed = document.getElementById("breed_id");
								for(var i=0;i<data.breedData.length;i++){
									var option=document.createElement("option");
									option.text=data.breedData[i].breedName;
									option.value=data.breedData[i].breedId;
									var sel=selectBreed.options[i];
									try{
										selectBreed.add(option,sel);
									 }catch (e){
										 selectBreed.add(option,sel.selectedIndex);
								  	}
								}
							}else{
								alert("This species is not Breed");
							}
						  }
						});
				});
			}); 
		</script>
</head>
<body>
<center>
<form name="frm1" id="frm1" action="../actions/pet_action.php" method="post">
<table border="0" width="75%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="2" align="left">&nbsp;&nbsp;&nbsp;รายละเอียดของสัตว์เลี้ยง</th>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
		<tr>
			<td align="right">ชื่อสัตว์เลี้ยง :</td>
			<td><input type="text" name="pet_name" id="pet_name" value="<?=$getListPet["pet_name"]?>"></td>
		</tr>
		<tr>
			<td align="right">อายุสัตว์เลี้ยง :</td>
			<td><input type="text" name="pet_age" id="pet_age" value="<?=$getListPet["pet_age"]?>" size="3" onkeypress="return numbersOnly(event, false);"></td>
		</tr>
		<tr>
			<td align="right">น้ำหนักของสัตว์เลี้ยง:</td>
			<td><input type="text" name="pet_weight" id=pet_weight value="<?=$getListPet["pet_weight"]?>" size="3" onkeypress="return numbersOnly(event, false);"></td>
		</tr>
		<tr>
			<td align="right">เพศสัตว์เลี้ยง :</td>
			<td>
				<input type="radio" name="pet_sex" id="pet_sex" value="M" checked="checked">เพศผู้
				<input type="radio" name="pet_sex" id="pet_sex" value="F"
				<?php 
					if($getListPet["pet_sex"] == "F"){
						echo 'checked="checked"';
					}
				?>
				>เพศเมีย
			</td>
		</tr>
		<tr>
			<td align="right">ประเภท :</td>
			<td>
				<select name="species_id" id="species_id">
					<option value="">--- เลือก ---</option>
					<?php 
						foreach($listSpecies as $value){
					?>
						<option value="<?=$value["species_id"]?>"
						<?php 
						if($value["species_id"]==$getListPet["species_id"]){
							echo 'selected="selected"'; 
						}
						?>
						><?=$value["species_name"]?></option>
					<?php
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td align="right">สายพันธุ์ :</td>
			<td>
				<select name="breed_id" id="breed_id">
					<option value="">--- เลือก ---</option>
					<?php 
						foreach($listBreed as $value){
					?>
						<option value="<?=$value["breed_id"]?>"
						<?php 
							if($value["breed_id"]==$getListPet["breed_id"]){
								echo 'selected="selected"'; 
							}
						?>
						><?=$value["breed_name"]?></option>
				<?php
						}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td align="right">เจ้าของสัตว์เลี้ยง :</td>
			<td>
				<?php 
				if($mode=="update"){
				?>
					<input type="text" name="fname" id="fname" readonly="readonly" value="<?=$listOwner["fname"]. " " .$listOwner["lname"]?>">
					<input type="hidden" name="owner_id"  id="owner_id" value="<?=$getListPet["owner_id"]?>">
				<?php
				}else{
				?>
				<select name="owner_id" id="owner_id" style="width: 200px;">
					<option>--- เลือก ---</option>
					<?php 
					foreach($listOwner as $value){
					?>
						<option value="<?=$value["owner_id"]?>"><?=$value["fname"] . " " .$value["lname"]?></option>
					<?php 
					}
				}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<input type="button" name="save_data" id="save_data" value="บันทึก">
			<input type="button" value="ยกเลิก"	onclick="window.location='pet_main.php';"></td>
		</tr>
	</tbody>
</table>
<input type="hidden" id="mode" name="mode" value=<?=$mode?>>
<input	type="hidden" id="pet_id" name="pet_id" value=<?=$_GET["pet_id"]?>>
</form>
</center>
</body>
</html>